import threading


class event_bus(object):
    _instance_lock = threading.Lock()

    def __init__(self):
        pass

    def __new__(cls, *args, **kwargs):
        if not hasattr(event_bus,"_instance"):
            with event_bus._instance_lock:
                if not hasattr(event_bus,"_instance"):
                    event_bus._instance = object.__new__(cls)
                    event_bus._observers = []

        return event_bus._instance

    def register(self,obj):
        self._observers.append(obj)
        pass

    def unregister(self,obj):
        self._observers.remove(obj)

    def post(self,type,msg):
        for o in self._observers:
            if hasattr(o,'event_bus_callback'):
                event_bus_callback = getattr(o, 'event_bus_callback')
                event_bus_callback(type,msg)
